#include "comm.h"

int main()
{
  // 创建一个key值
  key_t key = ftok(PATHNAME, PROJ_ID);
  if(key < 0)
  {
    perror("ftok faild:");
    exit(1);
  }
  // printf("key:%d", key);
  
  // client只需要获取共享内存即可, 使用IPC_CREAT获取已经创建好的共享内存
  int shmid = shmget(key, SIZE, IPC_CREAT);
  if(shmid < 0)
  {
    perror("shmget faild");
    exit(1);
  }
  // printf(", shmid:%d -> client process get share_memray\n", shmid);

  // 链接进程和共享内存
  char* mem = (char*)shmat(shmid, NULL, 0);
  printf("client process attach success\n");
  // sleep(5);
  /*-----通信逻辑-----*/ 
  const char* arr = "I am process A";
  int i = 0;
  for(i = 0; i < strlen(arr); i++)
  {
    mem[i] = arr[i];
  }

  // 取消链接
  shmdt(mem);
  printf("client process detaches success\n");
  // sleep(5);

  return 0;
}
